<?php

class Sessions extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('session');
    }

    function login() {
        $this->load->view('sessions/header');
        $this->load->view('sessions/login');
        $this->load->view('sessions/footer');
    }

    function authenticate() {
        $this->load->model('user', '', true);
        $user = $this->input->post('user');
        $userData = $this->user->authenticate($user['email'], $user['password']);
      if (count($userData)) {
            $credentials = array('loggedin' => true, 'user_id' => $userData->id, 'data' => $userData);
            $this->session->set_userdata($credentials);

            redirect('admin');
        } else {
  
           redirect('sessions/login_fail');
        }
    }

    function login_fail() {
        $data['page_title'] = 'Nombre de usuario y/o contraseÃ±a incorrectos';
        $this->load->view('sessions/header');
        $this->load->view('sessions/login_fail', $data);
        $this->load->view('sessions/footer');
    }

    function logout() {
        $this->session->unset_userdata('loggedin');
        redirect(base_url());
    }

    function user_login() {
        $this->load->model('user', '', true);
        $email = $this->input->post('email');
        $password = $this->input->post('password');

        $userData = $this->user->authenticate($email, $password);
        if ($userData) {
            $credentials = array('loggedin' => true, 'user_id' => $userData->id, 'data' => $userData);
            $this->session->set_userdata($credentials);
            $this->session->set_userdata($email);
            $redireccionar = $this->input->post('redirect');
            if($redireccionar == "" ){
                
            redirect('/');    
                
            }else{
                
                redirect($redireccionar);
            }
            
        } else {
            
            redirect('sessions/user_login_fail');
        }
    }

    function show_login() {
        $this->load->view('home/header.php');
        $this->load->view('home/user_login.php');
        $this->load->view('home/footer.php');
    }

    function user_login_fail() {
        $data['error_login'] = TRUE;
        $this->load->view('home/header.php');
        $this->load->view('home/index.php', $data);
        $this->load->view('home/footer.php');
    }

    function user_logout() {
        $this->session->sess_destroy();
        redirect('', 'refresh');
    }

    function login_publicar() {
        $this->load->model('user', '', true);
        $email = $this->input->post('email');
        $password = $this->input->post('password');

        $userData = $this->user->authenticate($email, $password);
        if ($userData) {
            $credentials = array('loggedin' => true, 'user_id' => $userData->id, 'data' => $userData);
            $this->session->set_userdata($credentials);
            $this->session->set_userdata($email);
            redirect('publica/publicar_curso');
        } else {
            redirect('sessions/user_login_fail');
        }
    }
    function frm_resgistrar(){
          $this->load->view('registro/registro_usuario');
        
        
    }
    function registro(){
                 $this->load->model("user");
                $this->load->library('form_validation');
                $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$this->form_validation->set_rules('nombre', 'Nombre', 'trim|required');
                $this->form_validation->set_rules('apellido', 'Apellido', 'trim|required');
                $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
                $this->form_validation->set_rules('password_1', 'Password', 'trim|required');
                $this->form_validation->set_rules('password_2', 'Repetir', 'trim|required');
                if ($this->form_validation->run() == FALSE)
                {
                    $this->frm_resgistrar();
                    
                }else{
                   $arrusuario = array(
                       
                       "email"=>$this->input->post('email'),
                       "password"=>  md5($this->input->post('password_1')),
                       "nombre"=>  $this->input->post('nombre'),
                       "apellido"=>  $this->input->post('apellido'),
           
                       
                       );
                 $this->user->guardarUser($arrusuario);
                  echo "<H2>SE REGISTRO CORRECTAMENTE</H2>";
                   
                }
                
                
                
                
    }
    
}

?>